<template>
	<view style="background-color: whitesmoke;">
		<cu-custom bgColor="bg-gradual-blue" :isBack="true"><block slot="backText">新增项目</block><block slot="content"></block></cu-custom>
		
		<form v-model="project" ref="form">
			<view class="cu-form-group margin-top" >
				<input placeholder="请输入项目名称" name="name" v-model="project.projectName"></input>
			</view>
		
			<view class="cu-form-group" :class="menuArrow ? 'arrow' : ''" @click="selectStaff" style="display: flex; justify-content: space-between;">
			  <view class="content" style="display: flex; align-items: center;">
			    <text class="">参与人</text>
			  </view>
			  <view class="action" style="display: flex; align-items: center;">
			    <view class="cu-avatar-group" v-for="item in ids" :key="item.id" style="padding: 0;">
			      <view class="cu-avatar round sm" :style="{ 'background-image': 'url(' + item.avatar + ')' }" style="margin-left: -10px;"></view>
			    </view>
			    <text class="text-grey text-sm">{{ ids.length }} 人</text>
			  </view>
			</view>
			<view class="cu-form-group">
				<view class="title">模板名称：</view>
				<input placeholder="请输入模块名称" name="name" v-model="project.moduleName">
			</view>
			<view class="cu-form-group">
				
			</view>
			
			<button @click="submit" type="primary" form-type="submit" style="width: 90%;top: 180px;justify-content: center;">
				创建
			</button>
		</form>
		
		<text style="padding: 35px;"></text>
	</view>
</template>

<script>
	import request from '../../common/request';
	import common from 'common/common.js';
	export default {
		data() {
			return {
				ids: [],
				menuArrow:'',
				project:{},
				staff:{},
			}
		},
		onLoad() {
			this.staff = uni.getStorageSync("staff");
		},
		onShow() {
			this.ids = uni.getStorageSync("ids");
			uni.removeStorageSync("ids");
		},
		methods: {
			// 选择参与人员
			selectStaff(){
				// 选中了之后，回显
				uni.navigateTo({
					url:'/pages/addTask/selectStaff?ids='+ encodeURIComponent(JSON.stringify(this.ids)),
				})
			},
			__e(){},
			// 添加提交
			submit(){
				if(!this.project.projectName){
					uni.showToast({
						title:"项目名不能为空",
						duration: 2000,
						icon:"error"
					})
					return;
				}
				if(!this.project.moduleName){
					uni.showToast({
						title:"模块名不能为空",
						duration: 2000,
						icon:"error"
					})
					return;
				}
					this.project.states = 0;
				if(this.ids){
					this.staff.states = true;
					if(!this.ids.some(obj => JSON.stringify(obj) === JSON.stringify(this.staff))){
						this.ids.push(this.staff);
					}
					this.project.ids = this.ids.map(item => item.id);
					console.log(this.project.ids);
				} else{
					this.ids =[];
					this.ids.push(this.staff);
					this.project.ids = this.ids.map(item => item.id);
				}
				var _this = this;
				request({
					url: common.api_base_url+'/okrproject',
					data:{
						"project": _this.project
					},
					header:{
					},
					method: 'PUT',
					success: res => {
						if(res.data.success){
							
							// 给任务成员发送消息
							if(_this.ids){
								// 去掉最后一个，不给自己发消息
								_this.ids.pop();
								var touserTmp = _this.ids.map(item => item.userId);
								var touser = touserTmp.join("|");
								// MessageSender.sendMessage(touser)
								//   .then(response => {
								// 	console.log('Message sent successfully:', response);
								// 	// 在这里可以处理发送成功的逻辑
								//   })
								//   .catch(error => {
								// 	console.error('Error sending message:', error);
								// 	// 在这里可以处理发送失败的逻辑
								//   });
							}
							_this.ids = [];
							
							uni.navigateBack({
							    delta: 1,
							    success: function () {
							        const pages = getCurrentPages(); //获取当前页面栈
							        const prevPage = pages[pages.length - 1]; //获取上一个页面实例对象
							        prevPage.onLoad(); //调用上一个页面的onLoad方法
							    }
							});
						} else {
							uni.showToast({
								title: res.data.message,
								duration: 2000,
								icon:"error"
							})
						}
						_this.project={
							projectName: null,
							staffIds: [],
							moduleName: null
						};
					},
					fail: function(e) {
						uni.showToast({
							title:"唉哟，程序错误了["+JSON.stringify(e)+"]",
							icon:"loading"
						})
					},
				});
				
			},
			
		}
	}
</script>

<style>

</style>
